//
//  TokenViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/30.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "TokenViewController.h"
#import "WithdrawActionViewController.h"
#import "HttpTokenAction.h"
#import "TokenInfo.h"
#import "TokenDetailViewController.h"
#import "DeblockingViewController.h"

@interface TokenViewController ()
@property (nonatomic,strong) UILabel *totalLabel;
@property (nonatomic,strong) NSArray *listArray;
//@property (nonatomic,strong) NSMutableArray *resultArray;
@property (nonatomic,strong) TokenInfo *tokenInfo;

@end

@implementation TokenViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.tableHeaderView = [self getHeaderView];
    self.listArray = @[@"可用",@"解锁中",@"已锁定"];
    self.resultArray = [NSMutableArray array];
    self.tableView.separatorStyle = 0;
    
    UIButton *cashButton = [UIButton new];
    [cashButton setTitle:@"提现" forState:UIControlStateNormal];
    [cashButton setTitleColor:DefaultBlueColor forState:UIControlStateNormal];
    [cashButton addTarget:self action:@selector(withdrawAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cashButton];
    cashButton.sd_layout.leftSpaceToView(self.view, 0).bottomSpaceToView(self.view, 0).widthIs(SCREEN_WIDTH/2).heightIs(44);
    
    UIButton *chargeButton = [UIButton new];
    [chargeButton setTitle:@"充值" forState:UIControlStateNormal];
    [chargeButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    chargeButton.backgroundColor = DefaultBlueColor;
    [self.view addSubview:chargeButton];
    chargeButton.sd_layout.leftSpaceToView(self.view, SCREEN_WIDTH/2).bottomSpaceToView(self.view, 0).widthIs(SCREEN_WIDTH/2).heightIs(44);
    [self loadData];
    
}

-(void)loadData{
    self.resultArray = [NSMutableArray array];
    [HttpTokenAction postSearchToken:^(id result, NSError *error) {
        if (!error) {
            self.tokenInfo = result;
            self.totalLabel.text = self.tokenInfo.totalAmount;
            [self.resultArray addObject:self.tokenInfo.ableAmount];
            [self.resultArray addObject:self.tokenInfo.deblockingAmount];
            [self.resultArray addObject:self.tokenInfo.lockAmount];
            [self.tableView reloadData];
        }
    }];
}

-(void)withdrawAction:(id)sender{
    WithdrawActionViewController *withdrawActionViewController = [[WithdrawActionViewController alloc] init];
    [self.navigationController pushViewController:withdrawActionViewController animated:YES];
}

-(UIView*)getHeaderView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180)];
    view.backgroundColor = DefaultBlueColor;
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"KEY";
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [view addSubview:titleLabel];
    titleLabel.sd_layout.topSpaceToView(view, StatusBarHeight).heightIs(44).leftSpaceToView(view, 100).rightSpaceToView(view, 100);
    
    UILabel *totalLabel = [UILabel new];
    totalLabel.font = [UIFont systemFontOfSize:30];
    totalLabel.textAlignment = NSTextAlignmentCenter;
    totalLabel.textColor = [UIColor whiteColor];
    totalLabel.text = @"0.0000";
    [view addSubview:totalLabel];
    totalLabel.sd_layout.topSpaceToView(titleLabel, 20).leftSpaceToView(view, 0).widthIs(SCREEN_WIDTH).heightIs(44);
    self.totalLabel = totalLabel;
    
    UIButton *button = [UIButton new];
    [button setTitle:@"查看更多" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [view addSubview:button];
    button.sd_layout.leftSpaceToView(view, 0).rightSpaceToView(view, 0).bottomSpaceToView(view, 0).heightIs(44);
    
    return view;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = 0;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.tag = 10;
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:titleLabel];
        titleLabel.sd_layout.rightSpaceToView(cell.contentView, 5).topSpaceToView(cell.contentView, 0).heightIs(44).widthIs(200);
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = DefaultCellLineColor;
        [cell.contentView addSubview:lineView];
        lineView.sd_layout.leftSpaceToView(cell.contentView, 0).bottomSpaceToView(cell.contentView, 0).widthIs(SCREEN_WIDTH).heightIs(1);
    }
    // Configure the cell...
    cell.textLabel.text = self.listArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    UILabel *titleLabel = [cell.contentView viewWithTag:10];
    if(self.resultArray.count == 0){
        titleLabel.text = @"0";
    }else{
        titleLabel.text = self.resultArray[indexPath.row];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        TokenDetailViewController *tokenDetailController = [[TokenDetailViewController alloc] initWithTitle:self.listArray[indexPath.row] withNumber:self.resultArray[indexPath.row] Block:^{
            [self loadData];
        }];
        tokenDetailController.tokenInfo = self.tokenInfo;
        [self.navigationController pushViewController:tokenDetailController animated:YES];
    }else if (indexPath.row == 1){
        DeblockingViewController *deblockingViewController = [[DeblockingViewController alloc] initWithDBlock:^{
            [self loadData];

        }];
        [self.navigationController pushViewController:deblockingViewController animated:YES];
    }else{
        TokenDetailViewController *tokenDetailController = [[TokenDetailViewController alloc] initWithTitle:self.listArray[indexPath.row] withNumber:self.resultArray[indexPath.row] Block:^{
            [self loadData];
        }];
        tokenDetailController.tokenInfo = self.tokenInfo;
        [self.navigationController pushViewController:tokenDetailController animated:YES];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
